//
//  MainTabBarViewController.swift
//  weibo27
//
//  Created by 董浩 on 16/4/19.
//  Copyright © 2016年 donghao. All rights reserved.
//

import UIKit

class MainTabBarViewController: UITabBarController {
    
    //调用按钮的点击方法
    func clickComposedButton() {
    
        print("点我了")
    
    
    }

    //MARK - 视图控制器的生命周期
    override func viewDidLoad() {
        super.viewDidLoad()

        addChildViewControllers()
        setupComposedButton()
        
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        tabBar.bringSubviewToFront(composedButton)
    }
    //MARK - 懒加载按钮
    private lazy var composedButton: UIButton = UIButton(
        imageName: "tabbar_compose_icon_add",
        backImageName: "tabbar_compose_button")
}




//MARK: - 设置界面
extension MainTabBarViewController {
    
    //设置撰写按钮的方法
    private func setupComposedButton() {
    //1，添加按钮
        tabBar.addSubview(composedButton)
        
        //2，调整按钮的位置
        let count = childViewControllers.count
        let w = tabBar.bounds.width / CGFloat(count)
        composedButton.frame = CGRectInset(tabBar.bounds, w * 2 - 1, 0)
    
        //3，给按钮添加点击方法
        composedButton.addTarget(self, action: "clickComposedButton", forControlEvents: UIControlEvents.TouchUpInside)
    
    
    
    }
    
    

    //设置控制器页面
    private func addChildViewControllers() {
        //设置图片渲染
        tabBar.tintColor = UIColor.orangeColor()
        
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }

    //添加控制器
    private func addChildViewController(vc:UIViewController,title:String,imageName:String) {
        
        //设置标题
        vc.title = title
        
        //设置图像
        vc.tabBarItem.image = UIImage(named: imageName)
        
        
        
        
        //设置导航控制器
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
    }
    


}